import os
import sys

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QLayout, QWidget, QGridLayout, QApplication, QMessageBox, QDesktopWidget, QFileDialog


def replaceWidget(layout: QLayout, widget: QWidget):
    clearLayout(layout)
    layout.addWidget(widget)


def clearLayout(layout: QLayout):
    while True:
        child = layout.takeAt(0)
        if child is None:
            break
        if child.widget():
            child.widget().setParent(None)
        del child


def nestWidget(parent: QWidget, child: QWidget):
    layout = parent.layout()
    if layout is None:
        layout = QGridLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        parent.setLayout(layout)
    replaceWidget(layout, child)


def moveToCenter(widget: QWidget):
    # desktop = QApplication.desktop()
    # widget.move((desktop.width() - widget.width()) >> 1, (desktop.height() - widget.height()) >> 1)
    screen = QDesktopWidget().screenGeometry()
    widget.move((screen.width() - widget.width()) >> 1,
                (screen.height() - widget.height()) >> 1)


def getMessageBox(text='', buttons=None, title='提示', informativeText=None):
    msg = QMessageBox()
    msg.setWindowTitle(title)
    msg.setText(text)
    # dialog = QMessageBox(QMessageBox.Information, '提示', "是否确认导入标准信号库",
    #                      QMessageBox.Yes | QMessageBox.No)
    # dialog.button(QMessageBox.Yes).setText('是')
    # dialog.button(QMessageBox.No).setText('否')
    if informativeText is not None:
        msg.setInformativeText(informativeText)
    if buttons is None:
        msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        msg.button(QMessageBox.Ok).setText('确定')
        msg.button(QMessageBox.Cancel).setText('取消')
        msg.setDefaultButton(QMessageBox.Ok)
    else:
        msg.setStandardButtons(buttons)
    return msg


def execMessageBox(text='', buttons=None, title='提示', informativeText=None):
    msg = getMessageBox(text, buttons, title, informativeText)
    return msg.exec_()


def appendStyleSheet(widget: QWidget, styleSheet):
    widget.setStyleSheet(
        widget.styleSheet() + styleSheet
    )


def getOpenFileName(fileTypes, tips='选择文件', relativePath=None):
    if relativePath is None:
        relativePath = os.getcwd()
    filePath, fileType = QFileDialog.getOpenFileName(
        None, tips, relativePath, fileTypes)
    return filePath, fileType


def getSaveFileName(fileTypes, tips='选择文件路径', relativePath=None):
    if relativePath is None:
        relativePath = os.getcwd()
    filePath, fileType = QFileDialog.getSaveFileName(
        None, tips, relativePath, fileTypes)  # 获得选择好的文件
    return filePath


def addMask(father: QWidget, child: QWidget):
    changeTransparency(father, 0.6)
    closeEvent = getattr(child, 'closeEvent', None)

    def proxy(a0: QtGui.QCloseEvent) -> None:
        if closeEvent:
            closeEvent(a0)
        changeTransparency(father, 1.0)
    setattr(child, 'closeEvent', proxy)


def changeTransparency(widget: QWidget, transparency):
    op = QtWidgets.QGraphicsOpacityEffect(widget)
    op.setOpacity(0)
    widget.setGraphicsEffect(op)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # if execMessageBox('???') == QMessageBox.Ok:
    #     pass
    if execMessageBox('确定删除事件？',title='删除事件') == QMessageBox.Ok:
        print(1)
    # sys.exit(app.exec_())


# class ShadowWidget(QWidget):
#     def __init__(self):
#         super().__init__()
#         palette = QPalette(self.palette())
#         palette.setColor(QPalette.Background, QColor(Qt.black))
#         self.setPalette(palette)
#         self.setWindowOpacity(0.2)
#         self.setWindowFlags(Qt.FramelessWindowHint)
#
#     @staticmethod
#     def addShadow(father: QWidget):
#         shadow = ShadowWidget()
#         setattr(father, 'shadowWidget', shadow)
#         shadow.setParent(father, Qt.FramelessWindowHint | Qt.Window)
#         shadow.showFullScreen()
#         shadow.setGeometry(0, 0, 1, 1)
#         # shadow.show()

